package com.bysc.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.cmsservice.entity.CmsMember;
import com.bysc.cmsservice.entity.CmsTeacher;
import com.bysc.cmsservice.entity.vo.CmsTeacherQueryVo;
import com.bysc.cmsservice.service.CmsTeacherService;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.bysc.commonutils.constant.RedisConstant.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/cmsservice/cms-teacher")
@Api(tags = "老师管理")
@Slf4j
public class CmsTeacherController {
    @Autowired
    private CmsTeacherService cmsTeacherService;


    /*
    * 第三方管理员更新老师信息
    * */
    @ApiOperation(value = "新增或更新老师（仅后台）")
    @PostMapping("/addOrUpdateTeacher")
    public Results addOrUpdateTeacher(
            @ApiParam(name = "cmsTeacher", value = "老师信息", required = true)
            @RequestBody CmsTeacher cmsTeacher, HttpServletRequest request) {
        try {
            String TOKEN_clubId= JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                cmsTeacher.setClubId(TOKEN_clubId);
            }
            cmsTeacherService.saveOrUpdate(cmsTeacher);
            return Results.ok().data("teacher", cmsTeacher).message(MessageConstant.EDIT_TEACHER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.EDIT_TEACHER_FAIL);
        }
    }


    /*
    * 根据clubId查询老师列表（前后台通用）
    * */
    @ApiOperation(value = "可带条件分页查询老师（前后台通用）")
    @PostMapping("getTeacherList/{page}/{limit}")
    public Results getTeacherList(
            @ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
            @ApiParam(name = "queryVo", value = "查询条件", required = false) @RequestBody(required = false) CmsTeacherQueryVo queryVo, HttpServletRequest request) {
        try {
            Page<CmsTeacher> pageParam = new Page<>(page, limit);
            LambdaQueryWrapper<CmsTeacher> wrapper = new LambdaQueryWrapper<CmsTeacher>()
                    .orderByDesc(CmsTeacher::getSort);
            if (queryVo != null) {
                String id = queryVo.getId();
                String name = queryVo.getName();
                String description = queryVo.getDescription();
                String title = queryVo.getTitle();
                String clubId = queryVo.getClubId();
                if (!StringUtils.isEmpty(clubId)) {
                    wrapper.eq(CmsTeacher::getClubId, clubId);
                }
                if (!StringUtils.isEmpty(id)) {
                    wrapper.eq(CmsTeacher::getId, id);
                }
                if (!StringUtils.isEmpty(name)) {
                    wrapper.like(CmsTeacher::getName, name);
                }
                if (!StringUtils.isEmpty(description)) {
                    wrapper.like(CmsTeacher::getDescription, description);
                }
                if (!StringUtils.isEmpty(name)) {
                    wrapper.like(CmsTeacher::getTitle, title);
                }
            }
            String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                wrapper.eq(CmsTeacher::getClubId, TOKEN_clubId);
            }
            IPage<CmsTeacher> cmsTeachers = cmsTeacherService.page(pageParam, wrapper);
            return Results.ok().data("list", cmsTeachers).message(MessageConstant.QUERY_TEACHER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_TEACHER_FAIL);
        }

    }


    /*
    *根据clubId删除对应老师Id
    * */
    @ApiOperation("删除老师（后台前端要传clubId）")
    @DeleteMapping("deleteTeacherById/{id}")
    public Results deleteTeacherById(
            @ApiParam(name = "id", value = "老师id", required = true)
            @PathVariable("id") String id) {
        try {
            cmsTeacherService.removeById(id);
            return Results.ok().message(MessageConstant.DELETE_TEACHER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.DELETE_TEACHER_FAIL);
        }
    }


}

